package cn.guxinhu.billsystem;

import cn.guxinhu.billsystem.user.pojo.BillUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Guxinhu
 * @create 2021-06-01 14:45
 */
@Controller
public class BillInterceptor implements HandlerInterceptor {
    @Autowired
    private BillUser user;

    @Override
    // 请求处理前执行
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        try {
            user = (BillUser) request.getAttribute("user");
            System.out.println(":" + user);
            if (user == null) {
                response.sendRedirect(request.getContextPath() + "/user/main");
                return false;
            }
        } catch (Exception e) {
            response.sendRedirect(request.getContextPath() + "/user/main");
            return false;
        }
        return true;
    }
}
